<template>
  <div class="dialog">
    <el-dialog title="添加部门" :visible.sync="isShow" width="50%" @close="closeclear">
      <el-form label-width="100px" :model="form" :rules="rules" ref="form">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="form.manager" @focus="gitUser">
            <el-option
              v-for="i in managerList"
              :key="i.id"
              :label="i.username"
              :value="i.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="form.introduce" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: center;">
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="sure">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { sysUserSimple,companyDepartment } from '@/api/treeitem.js'
export default {
  props: {
    allList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    const judeg = (rules, values, callback) => {
      const newArr = this.allList.filter(i => {
        if (i.pid === this.item.id) {
          return true
        }
      })
      const bol = newArr.some(i => {
        return i.name === values
      })
      bol ? callback(new Error('请输入正确内容')) : callback()
    }
    const judegCode = (rules, values, callback) => {
      const bol = this.allList.some(i => {
        return i.code === values
      })
      bol ? callback(new Error('请输入正确内容')) : callback()
    }
    return {
      isShow: false,
      item: '',
      managerList: [],
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 15,
            message: '请输入3到15范围内的数',
            trigger: 'blur'
          },
          { validator: judeg }
        ],
        code: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          },
          { validator: judegCode },
          {
            min: 3,
            max: 15,
            message: '请输入3到15范围内的数',
            trigger: 'blur'
          }
        ],
        manager: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          }
        ],
        introduce: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 15,
            message: '请输入3到15范围内的数',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    close () {
      this.isShow = false
    },
    async gitUser () {
      if (this.managerList.length == 0) {
        const res = await sysUserSimple()
        // console.log(res)
        this.managerList = res.data
      }
    },
    sure () {
      this.$refs.form.validate(val => {
        if (val) {
          this.$message.success('成啦!')
        }
      })
    },
    closeclear () {
      this.$refs.form.resetFields()
    }
  },
  mounted () {
    this.$bus.$on('addDialog', (val, item) => {
      this.isShow = val
      this.item = item
    })
  },
  beforeDestroy () {
    this.$bus.$off('addDialog')
  }
}
</script>

<style lang="scss" scoped></style>
